package com.mls.fivedistrict.controller.backend;

import com.github.pagehelper.PageInfo;
import com.mls.fivedistrict.common.PageFilter;
import com.mls.fivedistrict.common.RequestHolder;
import com.mls.fivedistrict.common.ServiceResponse;
import com.mls.fivedistrict.controller.BaseController;
import com.mls.fivedistrict.pojo.Advertising;
import com.mls.fivedistrict.server.AdvertisingService;
import com.mls.fivedistrict.util.validator.BeanValidator;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * Created by chenxiuxiang on 2018/4/14.
 */
@RestController("backenfAdvertisingController")
@RequestMapping("/api/manager")
public class AdvertisingController extends BaseController {
    @Autowired
    AdvertisingService advertisingService;

    @GetMapping("/v1/adversings")
    public ServiceResponse getAdversingList() {
        PageFilter pageFilter = RequestHolder.getPageFilter();
        List<Advertising> advertisingList = advertisingService.getAdversionListAndItem(pageFilter);
        PageInfo pageInfo = new PageInfo(advertisingList, 5);
        return ServiceResponse.createSuccessResponse(pageInfo);
    }


    @PostMapping("/v1/adversing")
    public ServiceResponse addAdvertising(@RequestBody Advertising advertising) {
//        if (StringUtils.isEmpty(advertising.getUrl())) {
//            return ServiceResponse.createErrorResponse("url不能为空");
//        }
//        if (StringUtils.isEmpty(advertising.getContent())) {
//            return ServiceResponse.createErrorResponse("内容不能为空");
//        }
        BeanValidator.check(advertising);
//        int i = advertisingService.addAdversing(advertising);
        boolean insert = advertisingService.insert(advertising);
        if (insert) {
            return ServiceResponse.createSuccessResponse("添加成功");
        }
        return ServiceResponse.createErrorResponse("添加失败");
    }

    @PutMapping("/v1/adversing/{adversingId}")
    public ServiceResponse eidtAdvertising(@PathVariable Long adversingId, @RequestBody Advertising advertising) {
//        Optional.ofNullable(adversingId).orElseThrow(() -> new CustomerException("广告不存在"));
//        Optional.ofNullable(advertising.getUrl()).orElseThrow(() -> new CustomerException("url不能为空"));
//        Optional.ofNullable(advertising.getContent()).orElseThrow(() -> new CustomerException("内容不能为空"));
        advertising.setId(adversingId);
        boolean update = advertisingService.updateById(advertising);
        if (update) {
            return ServiceResponse.createSuccessResponse("编辑成功");
        }
        return ServiceResponse.createErrorResponse("编辑失败");
    }


    @GetMapping("/v1/adversing/{adversingId}")
    public ServiceResponse eidtAdvertising(@PathVariable Long adversingId) {
//        Optional.ofNullable(adversingId).orElseThrow(() -> new CustomerException("广告不存在"));
        Advertising adversing = advertisingService.getAdversingAndItem(adversingId);
        if (adversing != null) {
            return ServiceResponse.createSuccessResponse(adversing);
        } else {
            return ServiceResponse.createErrorResponse("广告不存在");
        }
    }

    @DeleteMapping("/v1/adversing/{adversingId}")
    public ServiceResponse deleteAdvertising(@PathVariable Long adversingId) {
//        Optional.ofNullable(adversingId).orElseThrow(() -> new CustomerException("广告不存在"));
        Advertising advertising = new Advertising();
        advertising.setId(adversingId);
        advertising.setIsDelete(true);
//        int i = advertisingService.editAdversion(advertising);
        boolean update = advertisingService.updateById(advertising);
        if (update) {
            return ServiceResponse.createSuccessResponse("删除成功");
        }
        return ServiceResponse.createErrorResponse("编辑失败");
    }


}
